package com.li.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;

import com.li.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "9021000142669717";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCIoBqe7jqPAb4gY4/J9aSHh+xcVmD9k+kNSTgjLRvfdtAPrN/xNuDBOwHNwsfTZ7cNjv5p1mk05aSujzdKz1Y6MHVU2CBEZ73HVECQ5c5ThoorrXpgNtZE8OHLfVjntvHo3dA5X6GiU0BUt2rFnTL8ofSM1SAnCwQnWNnjSIKd19GnGJRPa3azkU5A6KN5ECw4/TfIJdPLsZG3N4v7WiHE/LZSPeMd29Ko/OoVKCiYxiedHCpYpPZa31a/9xTV7nUA9wwg7uZ/gszPt/CwXxmK6HvA176ix0k2SfYd1SlfARJM1APrFFw1YFofXG0p9mfUOfwY84pH3xB6+8zqyPm1AgMBAAECggEAbdn9TYhSgSVvj289766YaXi6bMvvFDRXEB2tC2t7m8NWKyResv3i3LrrClvwe2RK5JXyrXp+ANAcB1aTqIzo0cSgsmVMHHQlPR3bBCYiLTph4hiL6sxsn+XmRs00LKsF+qxOfbMMrQ2vDe6j5NwuYL5/eLTsMzhCULi6CGsGyo3MGtXSX7QwcYJLaDYNKB0teGryyuFvRtflS7hiDXsKM1irs/klR/578zxpGG9lTZi6o1zq+sUsyi75kzxHRTQeGWInClVDZBM72SRtTuU6Ha2Cn1L24a3OTZhrb+/HonwamNZOn7nFhfzHveTiggcuXpk4JWrVtj5rjTFTfdYZSQKBgQDROK+swkDpUlCJ1RKcR9/QUQUWv5ISHwxxMJiMaZU+rXDnq6lFVEm4e+gMk70617xNPjSraGAv8YrIr6wnWutyPNDR/xvaBSQa2WLJWM6WzMlfQdI53A+RR5dYXidB34zw3AEDto2xowrNWqxPv8Qbq81eeD2eI7onWpa6DHTT6wKBgQCnLDO65ZVa5QE8iyemSfbb4kEBpm4QyNsha/DyvA9CL2zL/rbtyY1rT16DuJ/TlTAYjkO5FV2LYxB2if4E8EhrQnZY2m+clhaFhGpS3Y1WlitQUd8AZVX2CwSKX1MnjC75JFTsI/edQGJKHCmPJsZ9wveCVz/SqMr51zw2bNUg3wKBgHN1zAUT4oygf4/BBZc3Vv14aM2IBQ15Ev24Ksd6UQGY57ECG9UF0fU8c+/U7F33xpP+G0TowgRTURq1T9UcKMkPZNIu7ejgY29pvkmNd9ziwNKP2XtSxUiQcKY0t4dz3izhzLGG4iVA6+ot+t+73XbD1f8nbiHCZ63mIT4lN6fnAoGAMb2pNrcRFZaGI6mUBpqDZTGQ/hizLzNUYEFp1A+4yoyp5pjHfUE3XvKiwLayd5YwVV4fMHb2+n5ja6HrfzSvl5qS9CUqiIDv+pJHwJhDcbwE70UtAUUlqqvkkfeDGoVPhLoWupyJFLUPidWPWBTFgq38Tz/+sYOfwW8Vb2w6jikCgYEAn43vCzk44pU5OSKYcbvMg1GdLvznDXmfq7RYkUMBgxBvDaYuoHD8u/M3WUqL+cwH6i3SzLIEgS5P7oZBppGokfqSeK/DMrPNbx5g4xUEdEIoGtBT978HE9kAEAVLLUNjVsS+o59CJCYn27ZrLHgT2x7VhybtOQSQGX8AOu6utxc=";
	// 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlF3K9EtpqsVZ4XBuynPgRAidgW/sBK9XzJYTABNZZVQNBmu05y+U0WhIQjzrEMuA1gxhn7ufXFG0+vPptROo5MnLXeCa9uJrA8LIcLP2vPAPsWLMYMMzoBKchuwTaeB/TtsqBdnEZbjFiXyPafX+Aq39XRQg9OpQs+meOwx6I2wRHBw8AHs2SI4G+fgO/RSQFxbDg+t31HBaqDRA3t3LiuBAefG6+SPRV9cku4oc3aTM0wVeDvUcDxfU5Cz0crpsgZuSxU6f0xYIXUuS/l8W2zQ30Eg/7+CQVW4OLkHfHJcFLI7RZD5ErQEduklff80kQP/jgoWE31WdNf3VWHKvbQIDAQAB";
	// 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url = "http://member.gulimall.com/memberOrder.html";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 自动关单时间
    private String timeout = "15m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        // 30分钟内不付款就会自动关单
        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\"" + timeout + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        return result;
    }
}
